$(document).ready(init);

/**
 * 查询用户信息
 */
function init() {
	var queryParams = {
	    
	};
	queryUser(queryParams, true);
	
	
	$('#searchButton').click(searchByName);
}

function searchByName() {
	var queryParams = {
		'employee.empName' : $.trim($('#empName').val()),
		'employee.loginAccount' : $.trim($('#loginAccount').val())
	};
	
	queryUser(queryParams, true);
}

/**
 * 查询用户信息
 */
function queryUser(queryParams, flag) {
		$('#reviewRegister').datagrid({
	    title:'',
	    iconCls:'icon-save',
	    singleSelect:true,
	    url:'iemployee-admin!queryEmployeeList.action',
	    idField: 'empID',
	    queryParams :queryParams,
	    columns:[[
	        {field:'empName',title:'真实姓名',width:fixWidth(0.08)},
	        {field:'loginAccount',title:'登录名',width:fixWidth(0.08)},
	        {field:'linkPhone',title:'联系电话',width:fixWidth(0.1)},
	        {field:'email',title:'邮箱',width:fixWidth(0.1)},
	        {field:'userType',title:'身份',width:fixWidth(0.1),
	        	formatter : function(value, rowData, rowIndex) {//0:院内、1:校内、2:校外
	        	    	var x = '<a href="#" onclick="showDetail('+ rowIndex + ',\'' + value +'\')">详情&nbsp;&nbsp;</a> ';
	        	    	
						if (value == '0') {
							return '院内';
				 		} else if (value == '1'){
				 			return '校内';
						} else if (value == '2') {
							return '校外';
						}
					}
	        },
	        {field:'deptName', title:'所属机构',width:fixWidth(0.1)},
	        {field:'workEntity', title:'工作单位', width:fixWidth(0.1)},
	        {field:'empType', title:'用户类型', width:fixWidth(0.1),
	        	formatter:function(value, rowData, rowIndex) {
						var content ='';
						if ('1' == value) {
							content = '注册用户';
						} else if ('2' == value) {
							content = '正式用户';
						} else if ('3' == value) {
							content = '课题组长';
						} else if ('4' == value) {
							content = '仪器管理员';
						} else if ('5' == value) {
							content = '实验室管理员';
						} else if ('6' == value) {
							content = '系统管理员';
						} else {
							content = '';
						}
						
						return content;
					}
	        },
	        {field:'stopState', title:'用户状态', width:fixWidth(0.1),hidden:false,
	        	formatter:function(value, rowData, rowIndex) {
						var content ='正常';
						if (0 == value) {
							content = '停用';
						}
						return content;
					}
	        },
	         {field:'isCheck', title:'是否审核', width:fixWidth(0.1),
	        	formatter:function(value, rowData, rowIndex) {
						var content ='已审核';
						if (0 == value) {
							content = '待审核';
						}
						return content;
					}
	        },
	        {field:'empID',title:'操作',width:fixWidth(0.1),align:'center',
	        	    formatter : function(value, rowData, rowIndex) {
	        	    	var x = '<a href="#" onclick="showDetail('+ rowIndex + ',\'' + value +'\')">详情</a> ';
	        	    	var e = '';
	        	    	
	        	    	if (rowData.loginAccount != 'system') {
							if (rowData.stopState == 1) {
								e = '<a href="#" onclick="switchNotUse('+ rowIndex + ',\'' + value +'\')">禁用</a> ';
								return x + '&nbsp;&nbsp;&nbsp;&nbsp;' +  e;
					 		} else {
								e = '<a href="#" onclick="switchCanUse('+ rowIndex + ',\'' + value +'\')">启用</a> ';
							}	        	    		
	        	    	}
	        	    	
						return x + '&nbsp;&nbsp;&nbsp;&nbsp;' + e;
					}
			}
	    ]],
	    pagination:true,
	    rownumbers:true,
	    fitColumns:true
	});
	
	$('#sure').click(closeDetail);
}

function switchCanUse(index, value) {
	switchUser(index, value, '您是否确定启用', 'ok');
}

function switchNotUse(index, value) {
	switchUser(index, value, '您是否确定禁用', 'nok');
}

function switchUser(index, value, title, taidu) 
{
	 $.messager.confirm('审核', title ,function(r){
        if (r){
           var params = {
				'taidu' : taidu,
				empID : value
			}
			
			var option = {
		   		type: 'POST',
		   		url: 'iemployee!switchStopState.action',
		   		data: params,
		   		success : function(msg){
		   			if (taidu == 'success') {
		   				$.messager.alert('确定', '恭喜您操作成功', 'info');
		   			}
		   			
		   			if (taidu == 'fail') {
		   				$.messager.alert('确定', '操作失败，请稍后或刷新页面尝试', 'error');
		   			}
		   			
		   			$('#reviewRegister').datagrid('load', {});
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					$.messager.alert('确定', '处理失败：' + errorThrown, 'error')
				}
			}
			
			$.ajax(option); 
        }
     });
}
